<html>
<body>
<head>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style>
        #content {
            border: 1px solid #ccc;
            min-height: 100px;
        }
    </style>
</head>
<h2>Hello World!</h2>
<button onclick="findAll()">findAll</button><button onclick="clearAll()">clear</button><br/>
id:<input id="uid"><button onclick="findById()">findById</button><br/>
id:<input id="uid2"><button onclick="deleteById()">deleteById</button><br/>
age:<input id="age">firstName:<input id="firstName">lastName:<input id="lastName"><button onclick="save()">save</button><br/>
id:<input id="uid3">age:<input id="age3">firstName:<input id="firstName3">lastName:<input id="lastName3"><button onclick="update()">update</button><br/>

<span>result:</span>
<div id="content"></div>
<script>
    function clearAll() {
        $('input').val('');

    }
    function findAll() {
        $.ajax({
            url: 'http://localhost:8080/user/',
            dataType: 'text',
            success: function (data) {
                $('#content').html(data)
            }
        })
    }

    function findById() {
        var id = $('#uid').val()
        $.ajax({
            url: 'http://localhost:8080/user/' + id,
            dataType: 'text',
            success: function (data) {
                $('#content').html(data)
            }
        })
    }

    function deleteById() {
        var id = $('#uid2').val()
        $.ajax({
            url: 'http://localhost:8080/user/' + id,
            method: 'DELETE',
            dataType: 'text',
            success: function (data) {
                $('#content').html(data)
            }
        })
    }

    function save() {
        var age = $('#age').val();
        var firstName = $('#firstName').val();
        var lastName = $('#lastName').val();
        var data = {age: age, firstName: firstName, lastName: lastName}
        $.ajax({
            url: 'http://localhost:8080/user/',
            method: 'POST',
            data: data,
            dataType: 'text',
            success: function (data) {
                $('#content').html(data)
            }
        })
    }
    function update() {
        var id = $('#uid3').val();
        var age = $('#age3').val();
        var firstName = $('#firstName3').val();
        var lastName = $('#lastName3').val();
//        var data = {age: age, firstName: firstName, lastName: lastName}
        var params = '?age=' + age + '&firstName=' + firstName + '&lastName=' + lastName
        $.ajax({
            url: 'http://localhost:8080/user/' + id + params,
            method: 'PUT',
            dataType: 'text',
            success: function (data) {
                $('#content').html(data)
            }
        })
    }
</script>
</body>
</html>
